﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recDebtorform.aspx.cs" Inherits="SIPO.Web.Forms.Administration.Catalog.recDebtorform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Debtor Form</title>

    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>

    <script type="text/javascript">

        var Edit = '<%= Request.Params["edit"] %>';
        if (Edit == 'true') {
            $("#rowActive").hide();
        }

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

        $('#btnClose').click(function () {
            parent.$('#dialog').dialog("close");
        });
        
    </script>
    <script type="text/javascript" language = "javascript">

        $(document).ready(function () {

            $.validator.addMethod("regex", function (value, element, parameters) {
                var re = new RegExp(parameters);
                return re.test(value);
            });

            // 1. prepare the validation rules and messages.
            var rules = {
                txtCode: {
                    required: true,
                    maxlength: 10
                },
                txtNPWP: { regex: '[0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9]-[0-9][0-9][0-9].[0-9][0-9][0-9]' },
                txtName: "required",
                txtEmail: "required"
            };
            var messages = {
                txtCode: {
                    required: "Kode Perusahaan harus di isi!",
                    maxlength: "Panjang maksimal Kode Perusahaan adalah 10"
                },
                txtNPWP: "Format NPWP harus benar 00.000.000.0-000.000",
                txtName: "Nama Perusahaan harus di isi!",
                txtEmail: "Email Perusahaan harus di isi!"
            };

            $("#txtNPWP").inputmask('99.999.999.9-999.999');

            $("#txtName").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtZipCode").inputmask("Regex", { regex: "[0-9]*" });

            $("#txtPhone").inputmask("Regex", { regex: "[0-9+-]*" });

            $("#txtFax").inputmask("Regex", { regex: "[0-9-+]*" });

            $("#txtWebsite").inputmask("Regex", { regex: "[A-Za-z-.0-9_/: ]*" });

            $("#txtAddress1").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtAddress2").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            $("#txtAddress3").inputmask("Regex", { regex: "[A-Za-z-.0-9 ]*" });

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            // 3. Get the Code
            $("#ChkIsPerson").click(function () {
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/generatedebtorcode.ashx?IsPerson=") %>';
                var obj = new Object();

                var checkIsActive = document.getElementById("ChkIsPerson");

                if (checkIsActive.checked) {

                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + true,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            $("#txtCode").val(data.Code);
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });


                }
                else {
                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + false,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            $("#txtCode").val(data.Code);
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }

            });

            function ValidasiNPWP() {
                var npwp = $('#txtNPWP').val();
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtorbynpwp.ashx?npwp=' + npwp + '") %>';
                var handlerUrl2 = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getregistrationbynpwp.ashx?npwp=' + npwp + '") %>';
                if (npwp != "") {
                    jQuery.ajax({
                        type: 'POST',
                        url: handlerUrl,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            if (data.DebtorName != "") {
                                alert('NPWP sudah ada, silahkan coba yang lain !');
                                $('#txtNPWP').val('');
                                $('#txtNPWP').focus();
                            }
                            else {
                                jQuery.ajax({
                                    type: 'POST',
                                    url: handlerUrl2,
                                    contentType: 'application/json',
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.DebtorName != "") {
                                            alert('NPWP sudah ada, silahkan coba yang lain !');
                                            $('#txtNPWP').val('');
                                            $('#txtNPWP').focus();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }

            $('#txtNPWP').blur(function () {
                ValidasiNPWP();
            });

            $("#btnSave").click(function () {

                if (!validator.validate())
                    return;

                var code = $('#txtCode').val();
                var name = $('#txtName').val();
                var npwp = $('#txtNPWP').val();
                var alamat1 = $('#txtAddress1').val();
                var alamat2 = $('#txtAddress2').val();
                var alamat3 = $('#txtAddress3').val();
                var zipcode = $('#txtZipCode').val();
                if ($("#ddlRegion option:selected").val() != "")
                    var region = $("#ddlRegion option:selected").val();
                if ($("#ddlCity option:selected").val() != "")
                    var city = $("#ddlCity option:selected").val();
                var email = $('#txtEmail').val();
                var phone = $('#txtPhone').val();
                var fax = $('#txtFax').val();
                var website = $('#txtWebsite').val();

                var isactive = "0";
                var isindividual = "0";
                var checkIsActive = document.getElementById("chkIsActive");
                var checkIsIndividual = document.getElementById("ChkIsPerson");
                if (checkIsActive.checked) {
                    isactive = "1";
                }
                else {
                    isactive = "0";
                }

                if (checkIsIndividual.checked) {
                    isindividual = "1";
                }
                else {
                    isindividual = "0";
                }

                var id = $('#<%= hfID.ClientID %>').val();

                var objrecDebtor = new Object();
                objrecDebtor.DebtorID = id;
                objrecDebtor.DebtorCode = encodeURIComponent(code);
                objrecDebtor.DebtorName = encodeURIComponent(name);
                objrecDebtor.NPWP = encodeURIComponent(npwp);
                objrecDebtor.Address1 = encodeURIComponent(alamat1);
                objrecDebtor.Address2 = encodeURIComponent(alamat2);
                objrecDebtor.Address3 = encodeURIComponent(alamat3);
                objrecDebtor.ZipCode = encodeURIComponent(zipcode);
                objrecDebtor.RegionID = encodeURIComponent(region);
                objrecDebtor.CityID = encodeURIComponent(city);
                objrecDebtor.Email = encodeURIComponent(email);
                objrecDebtor.Phone = encodeURIComponent(phone);
                objrecDebtor.Fax = encodeURIComponent(fax);
                objrecDebtor.Website = encodeURIComponent(website);
                objrecDebtor.IsActive = encodeURIComponent(isactive);
                objrecDebtor.IsIndividual = encodeURIComponent(isindividual);
                jsonData = JSON.stringify(objrecDebtor);

                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/administration/catalog/recDebtorhandler.ashx") %>';

                if (id == 0) {
                    CallAjax(handlerUrl, "?method=insert", jsonData);

                }
                else {
                    CallAjax(handlerUrl, "?method=update", jsonData);

                }
                parent.$('#dialog').dialog('close');
                setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);
                return false;

            });

            $('#ddlRegion').change(function () {
                var regionId = $("#ddlRegion option:selected").val();
                $citys = $('#ddlCity');
                if (regionId > 0) {
                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getcity.ashx?region_id=") %>';

                    $.ajax({
                        type: 'POST',
                        url: handlerUrl + regionId,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (datas) {
                            $citys.get(0).options.length = 0;
                            $citys.get(0).options[0] = new Option("-- pilih kota --", "");
                            $.each(datas, function (i, data) {
                                $citys.append('<option value="' + data.CityID + '">' + data.CityCode + ' - ' + data.CityName + '</option>');
                            });
                        },
                        error: function () {
                            alert('Error!');
                        }
                    });
                }
                else {
                    $citys.get(0).options.length = 0;
                    $citys.get(0).options[0] = new Option("-- pilih kota --", "");
                }

            });


        });
    
    </script>
    <script type="text/javascript">

        function ClearText() {
            $('#txtCode').val('');
            $('#txtName').val('');
            $('#txtNPWP').val('');
            $('#txtAddress1').val('');
            $('#txtAddress2').val('');
            $('#txtAddress3').val('');
            $('#txtZipCode').val('');
            $("#ddlRegion option:selected").val('');
            $("#ddlCity option:selected").val('');
            $('#txtEmail').val('');
            $('#txtPhone').val('');
            $('#txtFax').val('');
            $('#txtWebsite').val('');
        }        
         
    </script>
</head>
<body onload="ClearText();">
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Kode Perusahaan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtCode" Enabled="false" MaxLength="10" runat="server" Width="200"></asp:TextBox>&nbsp;
            </td>
        </tr>
        <tr>
            <td>
                Nama Perusahaan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server" Width="300" MaxLength="100"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                NPWP
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNPWP" runat="server" Width="300" MaxLength="100"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Perorangan
            </td>
            <td>
                :
            </td>
            <td>
                <asp:CheckBox ID="ChkIsPerson" runat="server" />
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Alamat&nbsp;
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtAddress1" TextMode="MultiLine" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                <asp:TextBox ID="txtAddress2" TextMode="MultiLine" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                <asp:TextBox ID="txtAddress3" TextMode="MultiLine" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Kode Pos
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtZipCode" runat="server"  MaxLength="5" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Wilayah
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlRegion" runat="server" Width="300">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Kota
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlCity" runat="server" Width="300">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Email
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server" Width="300" Rows="2"></asp:TextBox>
                <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Telepon
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Fax
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtFax" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Website
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtWebsite" runat="server" Width="300" Rows="2"></asp:TextBox>
            </td>
        </tr>
        <tr id="rowActive">
            <td style="width: 50">
                Aktif
            </td>
            <td>
                :
            </td>
            <td>
                <asp:CheckBox ID="chkIsActive" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="button">
                        Save</button>
                    <button id="btnClose" type="button">
                        Close</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
